package com.shanling.shanlingcontroller.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linkplay.lpvr.avslib.LPAVSManager;
import com.linkplay.lpvr.avslib.LPAlexaAccount;
import com.linkplay.lpvr.lpvrcallback.AuthorizationCallback;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseMVPActivity;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.persenter.UserPersenter;
import com.shanling.shanlingcontroller.persenter.contract.UserContract;
import com.shanling.shanlingcontroller.utils.ToastUtils;
import com.shanling.shanlingcontroller.view.MyDilalogUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserActivity extends BaseMVPActivity<UserContract.Presenter> implements UserContract.View, MyDilalogUtils.OnDialogClickListener {

    @BindView(R.id.iv_userback)
    ImageView ivUserback;
    @BindView(R.id.rl_account)
    RelativeLayout rlAccount;
    @BindView(R.id.rl_modify)
    RelativeLayout rlModify;
    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.rl_alexa)
    RelativeLayout rlAlexa;
    @BindView(R.id.rl_alexalaguage)
    RelativeLayout rlAlexalaguage;
    @BindView(R.id.ll_alxe)
    LinearLayout llAlxe;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.bt_logout)
    Button btLogout;
    @BindView(R.id.pb_logout)
    ProgressBar pbLogout;
    private LPAlexaAccount mLPAlexaAccount;
    private MyDilalogUtils dilalogUtils;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_user;
    }

    @Override
    protected void initViewsAndEvents() {
        dilalogUtils = new MyDilalogUtils(this, R.style.myDialog,getString(R.string.logout), getString(R.string.logouthint));
        dilalogUtils.setOnDialogClickListener(this);
        tvName.setText(preferenceUtil.getUserName());
        int bleType = preferenceUtil.getBleType();//1 2895p  2  2895 3
        switch (bleType) {
            case 1:
                llAlxe.setVisibility(View.VISIBLE);
                break;
            case 2:
            case 3:
                llAlxe.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    protected void initData() {
        mLPAlexaAccount = LPAVSManager.getInstance(this).getAccount();
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @OnClick({R.id.iv_userback, R.id.rl_modify, R.id.rl_alexa, R.id.rl_alexalaguage, R.id.bt_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_userback:
                finish();
                break;
            case R.id.rl_modify:
                if(!TextUtils.isEmpty(preferenceUtil.getPassword())){
                    readyGo(ChangePasswordActivity.class);
                }else {
                    ToastUtils.showToast(this,R.string.WeChatlogin_cannotchangepassword);
                }
                break;
            case R.id.rl_alexa:
                if (mLPAlexaAccount == null) {
                    return;
                }
                int state = mLPAlexaAccount.getState();
                if (state == LPAlexaAccount.LpAlexaAccountState.LP_ALEXA_ACCOUNT_VALID) {
                    readyGo(TryActivity.class);
                } else {
                    mLPAlexaAccount.logIn(this, new AuthorizationCallback() {
                        @Override
                        public void onCancel() {
                            ToastUtils.showToast(UserActivity.this, R.string.cancle);
                        }

                        @Override
                        public void onSuccess() {
                            ToastUtils.showToast(UserActivity.this, R.string.alexa_logingsuccess);
                            readyGo(TryActivity.class);

                        }

                        @Override
                        public void onError(Exception e) {
                            Log.e("weiyu", "失败" + e.toString());
                            ToastUtils.showToast(UserActivity.this, R.string.alexa_logingfailed);
                        }
                    });
                }
                break;
            case R.id.rl_alexalaguage:
                readyGo(AlexaLanguageActivity.class);
                break;
            case R.id.bt_logout:
                dilalogUtils.show();
                dilalogUtils.setCancelable(false);
                break;
        }
    }


    @Override
    public void logoutSuccess() {
        preferenceUtil.setUserName("");
        preferenceUtil.setPassword("");
        preferenceUtil.setSessionId("");
        btLogout.setVisibility(View.VISIBLE);
        pbLogout.setVisibility(View.GONE);
        ToastUtils.showToast(this, R.string.logout_success);
        readyGoThenKill(LoginActivity.class);
    }

    @Override
    public void logoutFaild(String msg) {
        btLogout.setVisibility(View.VISIBLE);
        pbLogout.setVisibility(View.GONE);
        ToastUtils.showToast(this, R.string.Logout_failed);

    }

    @Override
    protected UserContract.Presenter createPresenter() {
        return new UserPersenter();
    }
    @Override
    public void onSure() {
        btLogout.setVisibility(View.GONE);
        pbLogout.setVisibility(View.VISIBLE);
        mLPAlexaAccount.logOut();
        mPersenter.logout();
    }
    @Override
    public void onCancel() {

    }

}
